package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.BrandApi;
import com.yoda.mall.model.entity.pms.Brand;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.BrandMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shawn
 * @date 2022年 12月 16日 15:05
 */
@DubboService
public class BrandApiImpl implements BrandApi {

    @Resource
    private BrandMapper brandMapper;


    //根据名称查询分类列表
    @Override
    public PageResult<Brand> findBrand(Integer pageNum, Integer pageSize,String keyword) {
        Page<Brand> brandPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword),Brand::getName,keyword);
        brandMapper.selectPage(brandPage, wrapper);
        return new PageResult<>(pageNum, pageSize, (int) brandPage.getTotal(), brandPage.getRecords());
    }

    @Override
    public void addBrand(Brand brand) {

        brandMapper.insert(brand);
    }

    //删除品牌
    @Override
    public void delete(Integer id) {
        brandMapper.deleteById(id);
    }

    @Override
    public List<Brand> listAll() {
        return brandMapper.selectList(null);
    }

    //批量删除
    @Override
    public void deleteIds(List<Integer> ids) {
       brandMapper.deleteBatchIds(ids);
    }

    //修改品牌制造商状态
    @Override
    public void updateStatus(Integer factoryStatus, List<Long> ids) {
        //添加查询条件,根据品牌id查询品牌集合
        LambdaQueryWrapper<Brand> qw = new LambdaQueryWrapper<>();
        qw.in(Brand::getId,ids);
        //调用mapper查询所有品牌集合
        List<Brand> brands = brandMapper.selectList(qw);
        //遍历集合,获取每个品牌数据
        for (Brand brand : brands) {
            //将factoryStatus状态设置进品牌
            brand.setFactoryStatus(factoryStatus);
            //添加查询条件
            LambdaQueryWrapper<Brand> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(Brand::getId,brand.getId());
            //修改品牌状态
            brandMapper.update(brand,qw1);
        }
    }

    //修改是否更新显示状态  showStatus 1或0
    @Override
    public void updateShowstatus(Integer showStatus, List<Long> ids) {
        //添加查询条件
        LambdaQueryWrapper<Brand> qw = new LambdaQueryWrapper<>();
        qw.in(Brand::getId,ids);
        //调用mappper查询所有的品牌数据
        List<Brand> brands = brandMapper.selectList(qw);
        //遍历brands
        for (Brand brand : brands) {
            //设置状态
            brand.setShowStatus(showStatus);
            //添加查询条件
            LambdaQueryWrapper<Brand> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(Brand::getId,brand.getId());
            brandMapper.update(brand,qw1);
        }
    }

    //更新品牌数据
    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateById(brand);
    }

    //根据编号查询品牌信息
    @Override
    public Brand findById(Long id) {
       return brandMapper.selectById(id);
    }


}

